// 公共

// 配置鸡地址
axios.defaults.baseURL = 'https://hmajax.itheima.net'

// 访问页面权限
function checkLogin() {
    if (!localStorage.getItem('hmjytoken')) {
        alertMsg('请先登录', false)
        setTimeout(() => {
            location.href = './login.html'
        }, 1000)
    }
}
// 退出登录
function woZhenShuai() {
    document.querySelector('#logout').addEventListener('click', function () {
        localStorage.removeItem('hmjytoken')
        localStorage.removeItem('hmjyuname')
        alertMsg('退出成功')
        setTimeout(() => {
            location.href = './login.html'
        }, 1500)
    })
    // shabi
    document.querySelector('.username').innerHTML = localStorage.getItem('hmjyuname') || ''
}

// 请求拦截器
axios.interceptors.request.use(config => {
    if (localStorage.getItem('hmjytoken')) config.headers.Authorization = localStorage.getItem('hmjytoken')
    return config
})

// 响应拦截器
axios.interceptors.response.use(response => {
    return response
}, error => {
    if (error?.response?.status == 401) {
        alertMsg(error?.response?.data?.message + '请重新登录', false)
        setTimeout(() => {
            location.href = './login.html'
        }, 1500)
    } else {
        alertMsg(error?.response?.data?.message, false)
    }
})